Stata |
您所在的位置:网站首页 › stata tabulate › Stata |
How do I create dummy variables?
Title
Creating dummy variables
Author
William Gould, StataCorp
A dummy variable is a variable that takes on the values 1 and 0; 1 means something is true (such as age < 25, sex is male, or in the category “very much”). Dummy variables are also called indicator variables. As we will see shortly, in most cases, if you use factor-variable notation, you do not need to create dummy variables. In cases where factor variables are not the answer, you may use generate to create one dummy variable at a time and tabulate to create a set of dummies at once. Using factor variables instead of generating dummy variablesI have a discrete variable, size, that takes on discrete values from 0 to 4 . tabulate size size | Freq. Percent Cum. ------------+----------------------------------- miniature | 19 19.00 19.00 small | 37 37.00 56.00 normal | 30 30.00 86.00 large | 12 12.00 98.00 huge | 2 2.00 100.00 ------------+----------------------------------- Total | 100 100.00If I want a dummy for all levels of size except for a comparison group or base level, I do not need to create 4 dummies. Using [U] factor variables, I may type . summarize i.sizeor use an estimator . regress y x i.sizeIf I want to use a dummy that is 1 if size is large (size==3) and 0 otherwise, I type . regress y x 3.sizeIf I want to make the comparison group, or base level, of size be size==3 instead of the default size==0, I type . regress y x ib3.sizeYou can also use factor-variable notation to refer to categorical variables, their interactions, or interactions between categorical and continuous variables. For example, I can specify the interaction of each level of size (except the base level) and the continuous variable x by typing . regress y x i.size#c.xThe c. instructs Stata that variable x is continuous. In all the cases above, you did not need to create a variable. Moreover, many of Stata's postestimation facilities, including in particular the margins command, are aware of factor variables and will handle them elegantly when making computations. There are some instances where creating dummies might be worthwhile. We illustrate these below. Using generate to create dummy variablesYou could type . generate young = 0 . replace young = 1 if age |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |